package com.squirrel.michale;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/22 5:01 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1324 {

    public List<String> printVertically(String s) {
        String[] words = s.split(" ");
        int longest = 0;
        for (int i = 0; i < words.length; i++) {
            longest = longest > words[i].length() ? longest : words[i].length();
        }

        List<String> verticalList = new ArrayList<>();
        for (int j = 0; j < longest; j++) {
            char[] first = new char[words.length];
            for (int i = 0; i < words.length; i++) {
                if (j < words[i].length()) {
                    first[i] = words[i].charAt(j);
                } else {
                    first[i] = ' ';
                }
            }
            String verFirst = new String(first);
            char[] chars = verFirst.toCharArray();
            int lastCharIndex = chars.length - 1;
            for (int i = chars.length-1; i >= 0; i--) {
                if (chars[i] != ' ') {
                    lastCharIndex = i;
                    break;
                }
            }
            if (lastCharIndex < chars.length - 1) {
                verFirst = verFirst.substring(0, lastCharIndex+1);
            }
            verticalList.add(verFirst);
        }
        return verticalList;
    }


    public static void main(String[] args) {
        LeetCode1324 leetCode1324 = new LeetCode1324();

        String s = "HOW ARE YOU";
        List<String> stringList = leetCode1324.printVertically(s);
        for (int i = 0; i < stringList.size(); i++) {
            System.out.println(stringList.get(i));
        }


        String s2 = "TO BE OR NOT TO BE";
        List<String> stringList2 = leetCode1324.printVertically(s2);
        for (int i = 0; i < stringList2.size(); i++) {
            System.out.println(stringList2.get(i));
        }
    }
}
